package com.hyg.module_report.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hyg.module_report.R;
import com.hyg.module_report.databinding.ItemHealthAdviceV2Binding;
import com.hyg.module_report.model.V2AdviceDetailData;
import com.hyg.module_report.model.V2HealthAdviceData;
import com.umeng.commonsdk.debug.E;

import java.util.List;

/**
 * @author: Administrator
 * @date: 2020/12/31
 */
public class V2HealthAdviceAdapter extends RecyclerView.Adapter<V2HealthAdviceAdapter.AdviceViewHolder> {
    List<V2AdviceDetailData> list;
    ItemHealthAdviceV2Binding binding;
    Context context;
    int index = 0;
    public V2HealthAdviceAdapter(List<V2AdviceDetailData> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public AdviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        this.context=parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_health_advice_v2, parent, false);
        AdviceViewHolder holder = new AdviceViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AdviceViewHolder holder, int position) {
        V2AdviceDetailData data = list.get(position);
        String title="【饮食推荐】";
        if (data.getCategoryName().equals("一般饮食")) {
            title="【饮食推荐】";
        } else if (data.getCategoryName().equals("药膳")) {
            title="【药膳推荐】";
        } else if (data.getCategoryName().equals("茶饮")) {
            title="【茶饮推荐】";
        }else{
            title="【"+data.getCategoryName()+"】";
        }
        if (position == 0) {
            binding.tvType.setText(title);
            index = 1;
        } else if (!data.getCategoryName().equals(list.get(position - 1).getCategoryName())) {
            binding.tvType.setVisibility(View.VISIBLE);
            index = 1;
            binding.tvType.setText(title);
        } else {
            binding.tvType.setVisibility(View.GONE);
            index++;
        }
        binding.tvTitle.setText(index + "." + data.getYsName());
        if (!TextUtils.isEmpty(data.getImageUrl())) {
            Glide.with(context).load(data.getImageUrl()).error(R.mipmap.tongue_default).into(binding.ivShowImage);
        } else {
            binding.ivShowImage.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(data.getRawMaterial())) {
            binding.rlContentItem1.setVisibility(View.VISIBLE);
            binding.tvContent1.setText("\u3000\u3000"+data.getRawMaterial().replace("\n", ""));
        }
        if (!TextUtils.isEmpty(data.getPractice())) {
            binding.rlContentItem2.setVisibility(View.VISIBLE);
            binding.tvContent2.setText("\u3000\u3000"+data.getPractice().replace("\n", ""));
        }
        if (!TextUtils.isEmpty(data.getOperationUsage())) {
            binding.rlContentItem3.setVisibility(View.VISIBLE);
            binding.tvContent3.setText("\u3000\u3000"+data.getOperationUsage().replace("\n", ""));
        }
        if (!TextUtils.isEmpty(data.getPosition())) {
            binding.rlContentItem4.setVisibility(View.VISIBLE);
            binding.tvContent4.setText("\u3000\u3000"+data.getPosition().replace("\n", ""));
        }
        if (!TextUtils.isEmpty(data.getEffect())) {
            binding.rlContentItem5.setVisibility(View.VISIBLE);
            binding.tvContent5.setText("\u3000\u3000"+data.getEffect().replace("\n", ""));
        }
        if (!TextUtils.isEmpty(data.getBeCareful())) {
            binding.rlContentItem6.setVisibility(View.VISIBLE);
            binding.tvContent6.setText("\u3000\u3000"+data.getBeCareful().replace("\n", ""));
        }
        if (!TextUtils.isEmpty(data.getPrinciple())) {
            binding.rlContentItem7.setVisibility(View.VISIBLE);
            binding.tvContent7.setText("\u3000\u3000"+data.getPrinciple().replace("\n", ""));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class AdviceViewHolder extends RecyclerView.ViewHolder {
        View view;

        public AdviceViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = ItemHealthAdviceV2Binding.bind(itemView);
            this.view = itemView;
        }

        public View getView() {
            return view;
        }
    }

}
